<template>
    <div class="company">
        <search-box
            :paramArray="paramArray"
            @search="search"
            @reset="doSearchMsg"
            >
            </search-box>
            <table-box
            :tableObj="tableObj"
            @changePage="changePage"
            >
            </table-box>
            <Modal
            v-model="showBigImg"
            width="600px"
            @confirm="showBigImg = false"
            >
            <div class="big-img">
                <img :src='`${$baiseImg}${showImg == 0 && currentC.license || showImg == 1 && currentC.cardFront || showImg == 2 && currentC.cardBack}`'/>
            </div>
            </Modal>
    </div>
</template>
<script>
import {getCompanyList, companySh} from '@/js/api'
import {getDb} from '@/js/common'
import Pop from './pop'
export default {
    name: 'company',
    components: {
        Pop
    },
    data () {
        return {
            currentC: {},
            showImg: 0,
            showBigImg: false,
            statusName: [
                '待审核',
                '审核通过',
                '已驳回'
            ],
            paramArray: [
                {
                    key: 'input',
                    value: '',
                    label: '企业名称',
                    placeholder: '请输入企业名称',
                    width: '',
                    selectList: [],
                    casArray: [],
                    type: 'corpName'
                },
                // {
                //     key: 'startEndTime',
                //     value: '',
                //     label: '起止时间',
                //     placeholder: '开始日期          -          结束日期',
                //     width: '',
                //     type: 'times',
                //     selectList: [],
                //     casArray: []
                // },
                // {
                //     key: 'select',
                //     value: '',
                //     label: '状态',
                //     placeholder: '请选择',
                //     width: '',
                //     selectList: [
                //         {
                //             label: '待审核',
                //             value: 0
                //         },
                //         {
                //             label: '审核通过',
                //             value: 1
                //         },
                //         {
                //             label: '已驳回',
                //             value: 2
                //         }
                //     ],
                //     type: 'status',
                //     casArray: []
                // }
                
            ],
            tableObj: {
                pageObj: {
                    pageSize: 10,
                    total: 11,
                    page: 1
                },
                list: [{content: '你好', status: 0}],
                columns: [
                    {
                        key: 'corpName',
                        title: '企业名称'
                    },
                    {
                        key: 'creditCode',
                        title: '企业统一代码'
                    },
                    {
                        key: 'license',
                        title: '营业执照',
                        render: (h, params) => {
                            return ('div', [
                                h('img', {
                                    attrs: {
                                         src: this.$baiseImg+ params.row.license
                                    },
                                    style: {
                                        width: '50px',
                                        cursor: 'pointer'
                                    },
                                    on: {
                                        click: () => {
                                            this.currentC = params.row
                                            this.showBigImg = true
                                            this.showImg = 0
                                        }
                                    }
                                })
                            ])
                        }
                    },
                    {
                        key: 'cardFront',
                        title: '身份证正反面',
                        render: (h, params) => {
                            return ('div', [
                                h('img', {
                                    attrs: {
                                         src: this.$baiseImg+ params.row.cardFront
                                    },
                                    style: {
                                        width: '50px',
                                        cursor: 'pointer'
                                    },
                                    on: {
                                        click: () => {
                                            this.currentC = params.row
                                            this.showBigImg = true
                                            this.showImg =  1
                                        }
                                    }
                                }),
                                h('img', {
                                    attrs: {
                                         src: this.$baiseImg+ params.row.cardBack
                                    },
                                    style: {
                                        width: '50px',
                                        cursor: 'pointer'
                                    },
                                    on: {
                                        click: () => {
                                            this.currentC = params.row
                                            this.showBigImg = true
                                            this.showImg =  2
                                        }
                                    }
                                }),
                            ])
                        }
                    },
                    {
                        key: 'applyID',
                        title: '申请人'
                    },
                    {
                        key: 'updatedAt',
                        title: '申请时间'
                    },
                    {
                        key: 'status',
                        title: '状态',
                        render: (h, params) => {
                            return ('div', [
                                h('span', {
                                    on: {
                                        click: () => {
                                            this.currentC = params.row
                                            this.showBigImg = true
                                            this.showImg = 0
                                        }
                                    }
                                }, this.statusName[params.row.status] || '')
                            ])
                        }
                    },
                    {
                        key: 'action',
                        title: '操作',
                        render: (h, params) => {
                            let first = [ h(Pop, {
                                    props: {
                                        confirm: true,
                                        title: '您确定要通过审核么？'
                                    },
                                    on: {
                                        oks: () => {
                                            this.currentC = params.row
                                            this.goSh()
                                        },
                                        cancel: () => {
                                        }
                                    }
                                }, [h('a', {
                                }, '审核')]),
                                h('span', {
                                    class: 'split'
                                }, '')]

                            let second = [
                                // h('a', {
                                //     on: {
                                //         click: () => {
                                //             this.show(params.row)
                                //         }
                                //     }
                                // }, '查看项目'),
                                // h('span', {
                                //     class: 'split'
                                // }, ''),
                                h('a', {
                                    on: {
                                        click: () => {
                                            this.goDetail(params.row)
                                        }
                                    }
                                }, '详情')]
                                let dos = params.row.status == 0 ? [...first, ...second] : [...second]
                            return h('div', dos);
                        }
                    }
                ]
            },
            searchObj: {
                corpName: '',
                upDatedAtStart: '',
                upDatedAtEnd: '',
                status: ''
            },
        }
    },
    mounted () {
        this.getList()
    },
    methods: {
        search (obj) {
            this.doSearchMsg(obj)
            this.getList()
            this.tableObj.pageObj.page = 1
        },
        doSearchMsg (obj) {
            const {times, corpName, status} = obj
            this.$set(this.searchObj,'corpName', corpName)
            if (times && times.length && times[0]) {
                this.$set(this.searchObj,'upDatedAtStart', this.$moment(times[0]).format('YYYY-MM-DD'))
                this.$set(this.searchObj,'upDatedAtEnd', this.$moment(times[1]).format('YYYY-MM-DD'))
            } else {
                this.$set(this.searchObj,'upDatedAtStart', '')
                this.$set(this.searchObj,'upDatedAtEnd', '')
            }
            if (status) {
                 this.$set(this.searchObj,'status', status)
            }
        },
        goSh () {
            const {currentC: {applyID}} = this
            let userName = getDb('userName')
            let token = getDb('token')
            let param  = {}
            param = {
                applyID,
                userName,
                token
            }
            companySh(param).then(r => {
                if (r.code == 200) {
                    this.$Message.success('审核通过')
                    setTimeout(() => {
                        this.getList()
                    }, 1000)
                } else {
                    this.$Message.error(r.msg)
                }
            })

        },
        changePage (page) {
            this.tableObj.pageObj.page = page
            this.getList()
        },
        goDetail (item) {
            this.$router.push({
                name: 'companyDetail',
                query: {
                    applyID: item.applyID
                }
            })
        },
        show (item) {
            this.$router.push({
                path: '/project/list',
                query: {
                    corpName: item.corpName
                }
            })
        },
        getList () {
            let param = {}
            let userName = getDb('userName')
            let token = getDb('token')
            const {tableObj: {pageObj: {page}}, searchObj: {corpName , upDatedAtStart, upDatedAtEnd, status}} = this
            param = {
                userName,
                token,
                currentPage: page,
                // upDatedAtStart: '2010-1-1'
            }
            if (corpName) param.corpName = corpName
            if (upDatedAtStart) param.upDatedAtStart = upDatedAtStart
            if (upDatedAtEnd) param.upDatedAtEnd = upDatedAtEnd
            param.status =  status !=0 ? status  : 0
            getCompanyList(param).then(r => {
                if (r.code == 200) {
                    this.tableObj.pageObj.total = r.data.count || 0
                    this.tableObj.list = r.data.list || []
                }
            })
        }
    }
}
</script>
<style lang="less" scoped>
.company {
    background: #fff;
    padding: 20px;
    /deep/.split {
        margin: 0 5px;
        display: inline-block;
        width:1px;
        height: 10px;
        background: #2d8cf0;
    }
    /deep/.ivu-table-wrapper {
        overflow: inherit;
    }
    
}
/deep/.big-img {
    width: 100%;
    text-align:center;
    img {
        display: inline-block;
        max-width: 100%;
    }
}
</style>